/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.job.task;

import io.renren.modules.sp.dao.SpMatchItemTeamUserDao;
import io.renren.modules.sp.entity.SpMatchItemTeamUserEntity;
import io.renren.modules.sys.dao.SysCommDao;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试定时任务(演示Demo，可删除)
 *
 * testTask为spring bean的名称
 *
 * @author Mark sunlightcs@gmail.com
 */
@Component("testTask")
public class TestTask implements ITask{
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private SysCommDao sysCommDao;
	@Autowired
	private SpMatchItemTeamUserDao userDao;

	@Override
	public void run(String params){
		logger.debug("TestTask定时任务正在执行，参数为：{}", params);
		this.chuli2();
	}


	public void  chuli(){
		String sql = "SELECT it.source_id,t.* FROM `sp_match_item_team_user` t " +
				"left join sp_match_item_team it on t.match_item_team_id = it.id " +
				"group by t.match_item_team_id having count(1) = 1";
		Map map = new HashMap();
		List<Map<String, Object>> itemList = sysCommDao.commList(sql, map);
		for(Map<String, Object> item : itemList){
			Integer sourceId = (Integer)item.get("source_id");
			if(sourceId!=null) {
				String sql2 = "select * from sp_match_item_team_user where type =2 and match_item_team_id = " + sourceId;

				List<Map<String, Object>> userList = sysCommDao.commList(sql2, map);

				SpMatchItemTeamUserEntity userEntity = null;
				for (Map<String, Object> user : userList) {
					userEntity = new SpMatchItemTeamUserEntity();
					userEntity.setMatchItemTeamId((Long)item.get("match_item_team_id"));
					userEntity.setTeamUserId((Long)user.get("team_user_id"));
					userEntity.setStatus(6);
					userEntity.setIsAutoScore("0");
					userEntity.setTeamId((Long)item.get("team_id"));
					userEntity.setUserId((Long)user.get("user_id"));
					userEntity.setType(2);
					userEntity.setCreateDate(new Date());
					userEntity.setRemark("自动导入");
					userDao.insert(userEntity);
				}

			}
		}
	}


	public void  chuli2(){
		String sql = "SELECT  it.source_id,it.id match_item_team_id,it.*" +
				" from  sp_match_item_team it " +
				" left join `sp_match_item_team_user` t  on t.match_item_team_id = it.id " +
				" where t.match_item_team_id is null ";
		Map map = new HashMap();
		List<Map<String, Object>> itemList = sysCommDao.commList(sql, map);
		for(Map<String, Object> item : itemList){
			Integer sourceId = (Integer)item.get("source_id");
			if(sourceId!=null) {
				String sql2 = "select * from sp_match_item_team_user where match_item_team_id = " + sourceId;

				List<Map<String, Object>> userList = sysCommDao.commList(sql2, map);

				SpMatchItemTeamUserEntity userEntity = null;
				for (Map<String, Object> user : userList) {
					userEntity = new SpMatchItemTeamUserEntity();
					userEntity.setMatchItemTeamId((Long)item.get("match_item_team_id"));
					userEntity.setTeamUserId((Long)user.get("team_user_id"));
					userEntity.setStatus(6);
					userEntity.setIsAutoScore("0");
					userEntity.setTeamId((Long)item.get("team_id"));
					userEntity.setUserId((Long)user.get("user_id"));
					userEntity.setType((Integer)user.get("type"));
					userEntity.setCreateDate(new Date());
					userEntity.setRemark("自动导入");
					userDao.insert(userEntity);
				}
			}
		}
	}

}
